package com.etone.smartAudit.controller;

import com.etone.smartAudit.annotation.LoginAdmin;
import com.etone.smartAudit.domain.Admin;
import com.etone.smartAudit.dto.accountInfo.AccountInfoDTO;
import com.etone.smartAudit.enums.AccountType;
import com.etone.smartAudit.service.AccountInfoService;
import com.etone.smartAudit.vo.PagerVO;
import com.etone.smartAudit.vo.accountInfo.AccountInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author: sovy
 * @date: 2020/11/12
 * @description:
 */
@AllArgsConstructor
@RestController
@Api(tags = "账号信息管理", value = "AccountInfoController")
public class AccountInfoController {

    private AccountInfoService accountInfoService;

    @ApiOperation(value = "账号列表《完》")
    @GetMapping("/accountInfo")
    public PagerVO<AccountInfoVO> list(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "设备名称") @RequestParam(required = false) String serverName,
            @ApiParam(value = "设备ip") @RequestParam(required = false) String serverIp,
            @ApiParam(value = "设备id") @RequestParam(required = false) Integer serverId,
            @ApiParam(value = "账号类型") @RequestParam(required = false) AccountType type,
            @ApiParam(value = "项目id") @RequestParam(required = false) Integer itemId,
            @ApiParam(value = "当前页") @RequestParam(required = false, defaultValue = "1") int page,
            @ApiParam(value = "每页显示数量") @RequestParam(required = false, defaultValue = "10") int pageSize
    ) {
        return accountInfoService.list(admin, serverId, serverName, serverIp, type, itemId, page, pageSize);
    }

    @ApiOperation(value = "账号详情《完》")
    @GetMapping("/accountInfo/{id}")
    public AccountInfoVO detail(
            @ApiParam(value = "账号id") @PathVariable Integer id
    ) {
        return accountInfoService.detail(id);
    }

    @ApiOperation(value = "增加账号《完》")
    @PostMapping("/accountInfo")
    public void add(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "账号信息表单") @RequestBody @Validated AccountInfoDTO accountInfoDTO
    ) {
        accountInfoService.save(admin, null, accountInfoDTO);
    }


    @ApiOperation(value = "编辑账号《完》")
    @PutMapping("/accountInfo/{id}")
    public void edit(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "账号id") @PathVariable Integer id,
            @ApiParam(value = "账号信息表单") @RequestBody @Validated AccountInfoDTO accountInfoDTO
    ) {
        accountInfoService.save(admin, id, accountInfoDTO);
    }

    @ApiOperation(value = "删除账号《完》")
    @DeleteMapping("/accountInfo/{id}")
    public void delete(
            @ApiParam(value = "账号id") @PathVariable Integer id
    ) {
        accountInfoService.delete(id);
    }
}
